const STATUS_MAP = {
    IN_TREATMENT: { name: '就诊中', class: 'status-active' },
    NEED_REVIEW: { name: '需复诊', class: 'status-review' },
    FOLLOW_UP: { name: '长期跟进', class: 'status-follow' },
    ARCHIVED: { name: '档案归档', class: 'status-archived' },
  };
  
  const getPatientListData = () => {
    const rawData = [
      { patientId: 'P00123', patientName: '李思涵', initial: '李', gender: '女', age: 25, lastVisitDate: '今天 10:30', phone: '13812345678', status: 'IN_TREATMENT' },
      { patientId: 'P00456', patientName: '张建国', initial: '张', gender: '男', age: 38, lastVisitDate: '2025-11-20', phone: '13987654321', status: 'NEED_REVIEW' },
      // ... 更多数据
      { patientId: 'P01122', patientName: '赵薇薇', initial: '赵', gender: '女', age: 31, lastVisitDate: '2025-07-01', phone: '13399998888', status: 'ARCHIVED' },
      { patientId: 'P01334', patientName: '孙明', initial: '孙', gender: '男', age: 55, lastVisitDate: '2025-06-01', phone: '18977776666', status: 'FOLLOW_UP' },
    ];
    return rawData.map(p => ({
      ...p,
      statusName: STATUS_MAP[p.status].name,
      statusClass: STATUS_MAP[p.status].class,
    }));
  };
  
  Page({
    data: {
      allPatientList: [], 
      filteredList: [],    
      currentStatus: 'ALL',
      searchKeyword: '',   
      statusTabs: [        
        { key: 'ALL', name: '全部档案', count: 0 },
        { key: 'IN_TREATMENT', name: '就诊中' },
        { key: 'NEED_REVIEW', name: '需复诊' },
        { key: 'FOLLOW_UP', name: '长期跟进' },
        { key: 'ARCHIVED', name: '已归档' },
      ],
    },
    onLoad() { this.fetchPatientList(); },
    fetchPatientList() {
      const list = getPatientListData();
      const counts = list.reduce((acc, p) => { acc[p.status] = (acc[p.status] || 0) + 1; return acc; }, {});
      const updatedTabs = this.data.statusTabs.map(tab => {
          tab.count = tab.key === 'ALL' ? list.length : counts[tab.key] || 0;
          return tab;
      });
      this.setData({ allPatientList: list, statusTabs: updatedTabs }, this.filterAndSearch);
    },
    onSearchInput(e) { this.setData({ searchKeyword: e.detail.value }, this.filterAndSearch); },
    switchStatusTab(e) { this.setData({ currentStatus: e.currentTarget.dataset.key }, this.filterAndSearch); },
    filterAndSearch() {
      const keyword = this.data.searchKeyword.toLowerCase();
      const status = this.data.currentStatus;
      let result = this.data.allPatientList;
      if (status !== 'ALL') { result = result.filter(p => p.status === status); }
      if (keyword) {
          result = result.filter(p => 
              p.patientName.toLowerCase().includes(keyword) || 
              p.patientId.toLowerCase().includes(keyword) || 
              (p.phone && p.phone.includes(keyword)) 
          );
      }
      this.setData({ filteredList: result });
    },
    viewPatientProfile(e) {
      // 点击跳转到患者档案详情页 (原 patient-detail)
      wx.navigateTo({ url: `/pages/patient-detail/patient-detail?id=${e.currentTarget.dataset.id}` });
    },
    addNewPatient() { wx.navigateTo({ url: '/pages/patient-registration/patient-registration' }); },
    showSortOptions() { wx.showActionSheet({ itemList: ['按最近就诊时间排序', '按项目类型筛选'], success: () => { wx.showToast({ title: '已应用筛选', icon: 'none' }); } }); }
  });